Everything Totally Explained


Ask & we'll explain, totally!
Multiple inheritance
Totally Explained


  FOR SALE!Either this or the left-hand panel are available for just $19.95 per
day, or you can have both for only $34.95! Contact us for details.  


View this entry using RSS

Everything about Multiple Inheritance totally explained

Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass. Languages that support multiple inheritance include: Eiffel, C++, Python, Perl, Curl, and Common Lisp (via CLOS).

Overview

Multiple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named StudentMusician to inherit from a class named Person, a class named Musician, and a class named Worker. This can be abbreviated StudentMusician : Person, Musician, Worker.
   Ambiguities arise in multiple inheritance, as in the example above, if for instance the class Musician inherited from Person and Worker and the class Worker inherited from Person. There would then be the following rules:
StudentMusician: Person, Musician, Worker Musician : Person, Worker Worker: Person
   If a compiler is looking at the class StudentMusician it needs to know whether it should join identical features together, or whether they should be separate features. For instance, it would make sense to join the "Age" features of Person together for StudentMusician. A person's age doesn't change if you consider them a Person, a Worker, or a Musician. It would, however, make sense to separate the feature "Name" in Person and Musician if they use a different stage name than their given name. The options of joining and separating are both valid in their own context and only the programmer knows which option is correct for the class they're designing.
   Languages have different ways of dealing with these problems of repeated inheritance.
  • Eiffel allows the programmer to explicitly join or separate features that are being inherited from superclasses. Eiffel will automatically join features together if they've the same name and implementation. The class writer has the option to rename the inherited features to separate them. Eiffel also allows explicit repeated inheritance such as A: B, B.
  • C++ requires that the programmer state which parent class the feature to use should come from for example "Worker::Person.Age". C++ doesn't support explicit repeated inheritance since there would be no way to qualify which superclass to use.
  • Perl uses the list of classes to inherit from as an ordered list. The compiler uses the first method it finds by depth-first searching the superclass list. Python has the same structure, but unlike Perl includes it in the syntax of the language.
  • CLOS allows full programmer control of method combination, and if that's not enough, the Metaobject Protocol gives the programmer a means to modify the inheritance, method dispatch, class instantiation, and other internal mechanisms without affecting the stability of the system.
  • Logtalk supports both interface and implementation multi-inheritance, allowing the declaration of method aliases that provide both renaming and access to methods that would be masked out by the default conflict resolution mechanism.
  • Curl allows only classes that are explicitly marked as shared to be inherited repeatedly. Shared classes must define a secondary constructor for each regular constructor in the class. The regular constructor is called the first time the state for the shared class is initialized through a subclass constructor, and the secondary constructor will be invoked for all other subclasses.
Java, Nemerle, C#, PHP, and Objective-C don't allow multiple inheritance; this results in no ambiguity. However, Java, Nemerle, C#, PHP version 5 and Objective-C allow classes to inherit from multiple interfaces, recreating some of the problems, for example the problem mentioned above.

Debate

There is debate as to whether multiple inheritance can be implemented simply and without ambiguity. It is often criticized for increased complexity and ambiguity, as well as versioning and maintenance problems it can cause (often summarized as the diamond problem). Detractors also point out multiple inheritance implementation problems such as not being able to explicitly inherit from multiple classes and the order of inheritance changing class semantics. There are languages that address all technical issues of multiple inheritance, but the main debate remains whether implementing and using multiple inheritance is easier than using single inheritance and software design patterns.
   Multiple Inheritance usually corresponds to the is-a relationship. It is a common mistake to use mutiltiple inheritance for the has-a relationship. For example, a Filter circuit may consist of Resistors, Capacitors and Inductors. It'll be a mistake to declare Filter as a subclass of Resistor, Capacitor and Inductor (multiple inheritance). Instead, it's better to declare component objects of resistors, capacitors and inductors within the new Filter class, and reference the component methods indirectly through these component objects.
   If the programming language you're using doesn't support Multiple Inheritance, the IS-A relationship can also be implemented as the HAS-A relationship. Instead of directly inheriting the superclasses (IS-A relationship), objects can be declared within the new class to indirectly access the old classes (HAS-A relationship).

Further Information

Get more info on 'Multiple Inheritance'.


External Link Exchanges

Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:

    <a href="http://multiple_inheritance.totallyexplained.com">Multiple inheritance Totally Explained</a>

Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
   As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.



Copyright © 2007-8 totallyexplained.com | Licensed under the GNU Free Documentation License | Site Map
This article contains text from the Wikipedia article Multiple inheritance (History) and is released under the GFDL | RSS Version